package in.muton.edushare.grading.panels.prevgrades.old;

import in.muton.edushare.grading.panels.prevgrades.old.StudentGradeEdit.NormalEnrolments;
import in.muton.edushare.grading.panels.prevgrades.old.StudentGradeEdit.UnlinkedEnrolments;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.academics.grades.SGCalculation;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Users;

public class StudentSemGradeView extends EdusharePanel {

	private IModel<Users> userModel;
	private IModel<Batches> batchModel;
	private int semNumber;

	public StudentSemGradeView(String id, IModel<Users> user,
			IModel<Batches> batch, int currSem, EduShareBasePage page) {
		super(id, page);
		this.userModel = user;
		this.batchModel = batch;
		this.semNumber = currSem;
		add(new EduShareURLPanel("userURLPanel", userModel));
		add(new Label("semesterNum", "Semester : " + (currSem + 1)));
		final NormalEnrolments n = new NormalEnrolments(getORMInterface(),
				userModel, currSem, batch);

		WebMarkupContainer wmc = new WebMarkupContainer("reqSubWMC") {
			@Override
			public boolean isVisible() {
				return n.getObject().size() > 0;
			}

			private static final long serialVersionUID = -5053837455606268954L;
		};
		add(wmc);

		wmc.add(new ListView<IModel<Enrolments>>("mandatoryCoursesLV", n) {
			private static final long serialVersionUID = 458507356095482273L;

			@Override
			protected void populateItem(ListItem<IModel<Enrolments>> arg0) {
				Enrolments e = arg0.getModelObject().getObject();
				IModel<CourseInstances> courseModel = new CayenneModel<CourseInstances>(
						e.getCourseInstance());
				arg0.add(new EduShareURLPanel("courseURL", courseModel));
				arg0.add(new CourseGradeLabel("grade", userModel, courseModel));
			}
		});

		final UnlinkedEnrolments u = new UnlinkedEnrolments(getORMInterface(),
				batch, currSem, userModel);
		wmc = new WebMarkupContainer("unlinkedSubWMC") {
			@Override
			public boolean isVisible() {
				return u.getObject().size() > 0;
			}

			private static final long serialVersionUID = 9084832058612939333L;
		};
		add(wmc);
		wmc.add(new ListView<IModel<Enrolments>>("unlinkedCoursesLV", u) {
			@Override
			protected void populateItem(ListItem<IModel<Enrolments>> arg0) {
				Enrolments e = arg0.getModelObject().getObject();
				IModel<CourseInstances> courseModel = new CayenneModel<CourseInstances>(
						e.getCourseInstance());
				arg0.add(new EduShareURLPanel("courseURL", courseModel));
				arg0.add(new CourseGradeLabel("grade", userModel, courseModel));
			}

			private static final long serialVersionUID = 6078244182006260423L;
		});
		initGrading();
	}

	private void initGrading() {
		SGCalculation sgc = new SGCalculation(userModel.getObject(), batchModel
				.getObject(), semNumber);
		int currCredits = sgc.getCredits();
		int creditsEarned = sgc.getSuccessfulCredits(getORMInterface());
		double sgpa = sgc.getSemGradePointAverage(getORMInterface());
		double cgpa = sgc.getCGPA(getORMInterface());
		int cumulativeCredits = sgc.getTotalCredits();
		int cumulativeCreditsEarned = sgc
				.getSuccessfulCumulativeCredits(getORMInterface());

		add(new Label("totalCredits", currCredits + ""));
		add(new Label("creditsEarned", creditsEarned + ""));
		add(new Label("sgpa", sgpa + ""));
		add(new Label("cumulativeCredits", cumulativeCredits + ""));
		add(new Label("cumulativeCreditsEarned", cumulativeCreditsEarned + ""));
		add(new Label("cgpa", cgpa + ""));
	}

	private static final long serialVersionUID = -642057425926437751L;
}
